package com.frontop.system.config;

import com.alibaba.fastjson.JSONArray;
import com.frontop.system.modules.user.model.SysDict;
import com.frontop.system.modules.user.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 启动时需要缓存到redis中的数据
 */
@Component
public class StartCacheConfig {
    private static final Logger log = LoggerFactory.getLogger("adminLogger");
    @Autowired
   private RedisTemplate redisTemplate;
    @Autowired
    private SysDictService sysDictService;
    @Bean
    void cacheDict(){
        List<SysDict> sysDicts = sysDictService.getDictAll();
        Map<String, String> map = new HashMap<>();
        sysDicts.forEach(dict ->{
            map.put(dict.getType(), JSONArray.toJSONString(dict.getSysDictVO()));
        });
        redisTemplate.opsForHash().putAll("dict",map);
        log.info("字典表缓存成功："+sysDicts.size());
    }
}
